#include "../include/Mylogger.hpp"
#include <log4cpp/FileAppender.hh>
#include <iostream>


using std::cout;
using std::endl;


namespace cdm{

Mylogger* Mylogger::_log = nullptr;
Category* Mylogger::_root = nullptr;
Mylogger::AutoRelase Mylogger::_au;

Mylogger::Mylogger(){
    //输出到控制台
    OstreamAppender *op = new OstreamAppender("console",&cout);

    //输出到文件
    FileAppender *fp = new FileAppender("file","../log/file.log");

    //布局设置
    PatternLayout *pt1 =new PatternLayout();
    pt1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout *pt2 = new PatternLayout();
    pt2->setConversionPattern("%d %c [%p] %m%n");

    op->setLayout(pt1);
    fp->setLayout(pt2);

    //设置记录器
    _root = &Category::getRoot();
    _root->setPriority(Priority::DEBUG);
    _root->addAppender(op);
    _root->addAppender(fp);
}

Mylogger* Mylogger::getMylogger(){
    if(_log == nullptr){
        _log = new Mylogger();
    }
    return _log;
}
void Mylogger::destroyLoger(){
    if(_log != nullptr){
       delete _log;
        _log = nullptr;
    }
}

void Mylogger::warn(const char * file, int line, const char * func, const char * msg){
    _root->warn("file:%s line:%d func:%s msg:%s",file,line,func,msg);
}

void Mylogger::warn(const char * file, int line, const char * func, const string msg){
    _root->warn("file:%s line:%d func:%s msg:%s",file,line,func,msg.c_str());
}

void Mylogger::error(const char * file, int line, const char * func, const char * msg){
    _root->error("file:%s line:%d func:%s msg:%s",file,line,func,msg);
}

void Mylogger::error(const char * file, int line, const char * func, const string msg){
    _root->error("file:%s line:%d func:%s msg:%s",file,line,func,msg.c_str());
}

void Mylogger::info(const char * file, int line, const char * func, const char * msg){
    _root->info("file:%s line:%d func:%s msg:%s",file,line,func,msg);
}

void Mylogger::info(const char * file, int line, const char * func, const string msg){
    _root->info("file:%s line:%d func:%s msg:%s",file,line,func,msg.c_str());
}

}//end of cdm


